package com.zlq.cn.config;

import com.alibaba.fastjson.support.retrofit.Retrofit2ConverterFactory;
import com.zlq.cn.entity.LieYingService;
import com.zlq.cn.entity.LieYingTerminal;
import lombok.Data;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.http.*;

import java.util.List;

@Configuration
public class LieYingConfig {

    @Bean
    public LieYingApi lieYingService() {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor(System.out::println)
                        .setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        return new Retrofit.Builder()
                .baseUrl("https://tsapi.amap.com/")
                .client(client)
                .addConverterFactory(Retrofit2ConverterFactory.create())
                .build()
                .create(LieYingApi.class);
    }

    public interface LieYingApi {

        /**
         * 创建服务
         */
        @POST("/v1/track/service/add")
        @FormUrlEncoded
        Call<LieYingResult<LieYingService>> serviceAdd(@Field("key") String key,
                                                       @Field("name") String name,
                                                       @Field("desc") String desc);

        /**
         * 删除服务
         */
        @POST("/v1/track/service/delete")
        @FormUrlEncoded
        Call<LieYingResult<String>> serviceDelete(@Field("key") String key,
                                                  @Field("sid") String sid);

        /**
         * 修改服务
         */
        @POST("/v1/track/service/update")
        @FormUrlEncoded
        Call<LieYingResult<LieYingService>> serviceUpdate(@Field("key") String key,
                                                          @Field("sid") String sid,
                                                          @Field("name") String name,
                                                          @Field("desc") String desc);

        /**
         * 查询服务
         */
        @GET("/v1/track/service/list")
        Call<LieYingResult<LieYingList<LieYingService>>> serviceList(@Query("key") String key);

        /**
         * 创建终端
         */
        @POST("/v1/track/terminal/add")
        @FormUrlEncoded
        Call<LieYingResult<LieYingTerminal>> terminalAdd(@Field("key") String key,
                                                         @Field("sid") int sid,
                                                         @Field("name") String name);
    }

    @Data
    public static class LieYingResult<T> {

        private int errcode;

        private String errmsg;

        private String errdetail;

        private T data;
    }

    @Data
    public static class LieYingList<T> {

        private List<T> results;
    }
}
